import json
import datetime
from django.http import HttpResponse
from app.models import *

# 获取全部角色信息   
def get_role_all(request):
    context = dict()
    try:
        # 查询Role表全部记录
        lstRoles = list(Role.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context


# 获取全部用户信息
def get_user_all():
    # 返回字典
    context = dict()
    try:
        # 查询User表全部记录
        lstRoles = list(User.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 获取全部仓库信息
def get_warehouse_all():
    # 返回字典
    context = dict()
    try:
        # 查询Warehouse表全部记录
        lstRoles = list(Warehouse.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context


# 获取全部物料信息
def get_material_all():
    # 返回字典
    context = dict()
    try:
        # 查询Material表全部记录
        lstRoles = list(Material.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp['in_time'] = lstRoles[i].in_time.strftime('%Y-%m-%d')
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 获取全部部门信息
def get_department_all():
    # 返回字典
    context = dict()
    try:
        # 查询Department表全部记录
        lstRoles = list(Department.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context
    

# 获取全部入库申请信息
def get_IB_all():
    # 返回字典
    context = dict()
    try:
        # 查询IB表全部记录
        lstRoles = list(IB.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp['creation_time'] = lstRoles[i].creation_time.strftime('%Y-%m-%d')
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
        # print(1)
    except:
        context['result'] = 0
    return context
    

# 获取全部出库申请信息
def get_OB_all():
    # 返回字典
    context = dict()
    try:
        # 查询OB表全部记录
        lstRoles = list(OB.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp['creation_time'] = lstRoles[i].creation_time.strftime('%Y-%m-%d')
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context
    
# 获取全部通知信息
def get_Notice_all():
    # 返回字典
    context = dict()
    try:
        # 查询Notice表全部记录
        lstRoles = list(Notice.objects.all())
        resData = []
        j = 1
        for i in range(len(lstRoles)-1,-1,-1):
            temp = {'id':j}
            temp = dict(temp,**lstRoles[i].__dict__) 
            temp['date'] = lstRoles[i].date.strftime('%Y-%m-%d')
            temp.pop('_state')
            j = j + 1 
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 获取全部物料类别信息   
def get_MaterialType_all():
    context = dict()
    try:
        # 查询MaterialType表全部记录
        lstRoles = list(MaterialType.objects.all())
        resData = []
        for i in range(len(lstRoles)):
            temp = {'id':i+1}
            temp = dict(temp,**lstRoles[i].__dict__)
            temp.pop('_state')
            resData.append(temp)
        # 封装到传递参数中
        context['Data'] = resData
        context['result'] = 1
    except:
        context['result'] = 0
    return context
